﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlyweightPattern
{
    /// <summary>
    /// 1  建立一个享元模式（Flyweight）
    /// 2  和单例模式的区别
    /// 3  享元模式在.net里面的应用
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训，今天是Eleven老师为大家带来的享元模式Flyweight");
                string freeTeacher = "eleven";
                string vipTeacher = "eleven";

                E e = new E();
                E e2 = new E();
                Console.WriteLine(object.ReferenceEquals(e, e2));

                Console.WriteLine("reeTeacher==vipTeacher?{0}", freeTeacher == vipTeacher);//判断的是值
                Console.WriteLine("freeTeacher.Equals(vipTeacher)?{0}", freeTeacher.Equals(vipTeacher));//判断的是值
                Console.WriteLine("reeTeacher==vipTeacher?{0}", object.ReferenceEquals(freeTeacher, vipTeacher));//判断的引用

                string word1 = "ele";
                string word2 = "ven";
                string teacher = string.Format("{0}{1}", word1, word2);

                Console.WriteLine("teacher==vipTeacher?{0}", object.ReferenceEquals(teacher, vipTeacher));


                //Show();
                //ShowEleven();
                new Action(Show).BeginInvoke(null, null);//异步调用
                new Action(ShowEleven).BeginInvoke(null, null);//异步调用
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }

        //private static E eCommon = new E();

        private static void ShowEleven()
        {
            BaseWord e = FlyweightFactory.GetWord(WordType.E);// new E();
            BaseWord l = FlyweightFactory.GetWord(WordType.L);//new L();
            //E e2 = new E();
            BaseWord v = FlyweightFactory.GetWord(WordType.V);//new V();
            //E e3 = new E();
            BaseWord n = FlyweightFactory.GetWord(WordType.N);//new N();

            for (int i = 0; i < 10; i++)
            {
                //Console.WriteLine("Teacher is {0}{1}{2}{3}{4}{5}", e.Display(), l.Display(),
                //    e2.Display(), v.Display(), e3.Display(), n.Display());
                Console.WriteLine("Teacher is {0}{1}{2}{3}{4}{5}", e.Display(), l.Display(),
                   e.Display(), v.Display(), e.Display(), n.Display());
            }
        }

        private static void Show()
        {
            BaseWord e = FlyweightFactory.GetWord(WordType.E);// new E();
            BaseWord l = FlyweightFactory.GetWord(WordType.L);//new L();
            //E e2 = new E();
            BaseWord v = FlyweightFactory.GetWord(WordType.V);//new V();
            //E e3 = new E();
            BaseWord n = FlyweightFactory.GetWord(WordType.N);//new N();

            for (int i = 0; i < 10; i++)
            {
                //Console.WriteLine("Teacher is {0}{1}{2}{3}{4}{5}", e.Display(), l.Display(),
                //    e2.Display(), v.Display(), e3.Display(), n.Display());
                Console.WriteLine("Teacher is {0}{1}{2}{3}{4}{5}", e.Display(), l.Display(),
                   e.Display(), v.Display(), e.Display(), n.Display());
            }
        }
    }
}
